﻿#include<iostream>
using namespace std;

class Base
{
public:
	virtual void Func1()
	{
		cout << "Func1()" << endl;
	}

	virtual void Func2()
	{
		cout << "Func2()" << endl;
	}

	void Func3()
	{
		cout << "Func3()" << endl;
	}
protected:
	int _b = 1;
	char _ch = 'x';
};

class Derive : public Base
{
public:
	virtual void Func2()//重写
	{
		cout << "Func2()" << endl;
	}

	virtual void Func4()//编译器在监视窗口不会显示，可以在内存窗口查看
	{
		cout << "Func4()" << endl;
	}
protected:
	int _d = 1;
};

int main() 
{
	//同类型的对象共用同一张虚表
	Base b1;
	Base b2;
	//不同类型的对象各有独自的虚表
	Derive d;

	return 0;
}

//虚函数和普通函数都存放在代码区(常量区)
//虚函数表(存放虚函数地址)vs下存放在代码区(常量区)

//int main()
//{
//	int i = 0;
//	static int j = 1;
//	int* p1 = new int;
//	const char* p2 = "xxxxxxxx";
//	printf("栈:%p\n", &i);
//	printf("静态区:%p\n", &j);
//	printf("堆:%p\n", p1);
//	printf("常量区:%p\n", p2);
//
//	Base b;
//	Derive d;
//	Base* p3 = &b;
//	Derive* p4 = &d;
//	printf("Base虚表地址:%p\n", *(int*)p3);//取头四个字节的地址
//	printf("Derive虚表地址:%p\n", *(int*)p4);//取头四个字节的地址
//	printf("虚函数地址:%p\n", &Base::Func1);
//	printf("普通函数地址:%p\n", &Base::Func3);
//
//	return 0;
//}
